﻿using DataAccess;
using DataAccess.Exceptions;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.WPF
{
    public class ClassroomObjectManager
    {
        public Classroom newClassroom(String code, int capacity, char type, char status)
        {
            try
            {
                if (capacity <= 0)
                {
                    throw new ValidationException("La capacidad de aulas no pueden ser menor o igual a 0");
                }
                if (String.IsNullOrEmpty(code))
                {
                    throw new ValidationException("El codigo no puede ser vacio");
                }                
            
                if (ClassroomDataAccess.getClassroomByCode(code) == null)
                {
                    Classroom newSubject = ClassroomDataAccess.newClassroom(code, capacity, type, status);
                    if (newSubject == null)
                    {
                        throw new BusinessException("El aula no se creo correctamente, intente de nuevo más tarde");
                    }
                    else
                    {
                        return (newSubject);
                    }
                }
                else
                {
                    throw new BusinessException("Ya existe un aula con el codigo: " + code);
                }
            }
            catch (Exception exception)
            {
                throw new BusinessException("Error: " + exception.Message);
            }
        }

        public Classroom modifyClassroom(String code, int capacity, char type, char status)
        {
            try
            {
                if (capacity <= 0)
                {
                    throw new ValidationException("La capacidad de aulas no pueden ser menor o igual a 0");
                }
                if (String.IsNullOrEmpty(code))
                {
                    throw new ValidationException("El codigo no puede ser vacio");
                }
                
                Classroom newSubject = ClassroomDataAccess.modifyClassroom(code, capacity, type, status);
                if (newSubject == null)
                {
                    throw new BusinessException("El aula no se creo correctamente, intente de nuevo más tarde");
                }
                else
                {
                    return (newSubject);
                }                
            }
            catch (Exception exception)
            {
                throw new BusinessException("Error: " + exception.Message);
            }
        }
    }
}
